<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri='http://www.springframework.org/security/tags' prefix='sec'%>
<!DOCTYPE html>
<html>
	<head>
		<jsp:include page="../../inc/inc.jsp"></jsp:include>
		<script type="text/javascript" src="<%=request.getContextPath() %>/easyui/datagrid-detailview.js"></script>
		<script type="text/javascript">
			var mainIndex;	//所选主表INDEX
			$(document).ready(function () { 
				$("#dict_list").datagrid({ 
					striped: true, 
					border: false,
					loadMsg: '正在加载数据......',
					collapsible:false,		//是否可折叠的 
					fit: true,				//自动大小 
					url:"dict_list", 
					idField:'id', 
					singleSelect:true,//是否单选 
					pagination:true,//分页控件 
					remoteSort:false,
					columns:[[{
						field:'dictName',
				        title:'名称',
				        sortable:true,
				        width:200,
				        halign:'center'
					},{
						field:'dictCode',
				        title:'字典代码',
				        sortable:true,
				        width:200,
				        halign:'center'
					},{
						field:'dictStatus',
				        title:'状态',
				        sortable:true,
				        width:90,
				        halign:'center',
				        formatter : function(value, row, index) {
				        	switch (value) {
							case "0":
								return '正常';
							case "1":
								return '停用';
							}
						}
					},{
						field:'remark',
				        title:'备注',
				        width:200,
				        halign:'center'
					},{
						field:'parentId',
				        title:'上级字典',
				        hidden:'true'
					},{
		 				field:'action',
				        title:'操作',
				        width:180,
				        halign:'center',
				        formatter : function(value, row, index) {
							var str = '&nbsp;';
							<sec:authorize ifAnyGranted='B_ZDGL_ADD'>
								str += $.formatString('<a href="javascript:void(0)" onclick="addDict(\'{0}\');" >增加子字典</a>', row.id);
								str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
							</sec:authorize>
							<sec:authorize ifAnyGranted='B_ZDGL_EDIT'>
								str += $.formatString('<a href="javascript:void(0)" onclick="editDict(\'{0}\');" >编辑</a>',index);
								str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
							</sec:authorize>
							<sec:authorize ifAnyGranted='B_ZDGL_DEL'>
								str += $.formatString('<a href="javascript:void(0)" onclick="destroyDict(\'{0}\');" >删除</a>',row.id);
							</sec:authorize>
							return str;
						}
					 }]],
					toolbar: '#toolbar'
				});
				
				
				//子字典数据
				$('#dict_list').datagrid({
					view: detailview,
					detailFormatter:function(index,row){
		   				return '<div style="padding:2px"><table id="ddv-'+index+'"></table></div>';
					},
		           	onExpandRow: function(index,row){
						mainIndex = index;
						$('#ddv-'+index).datagrid({
			 				url:"dict_sub?id="+row.id,
			         		singleSelect:true,
		        			rownumbers:true,
		         			loadMsg:'正在加载数据......',
		          			height:'auto',
		             		columns:[[{
		             			field:'dictName',
		                		title:'名称',
		                        halign:'center',
		                        width:200
		                    },{
		                        field:'dictCode',
		                        title:'字典代码',
		                        halign:'center',
		                        width:157
		                    },{
		                        field:'dictValue',
		                        title:'字典值',
		                        halign:'center',
		                        width:100
		                    },{
		                        field:'dictStatus',
		                        title:'状态',
		                        width:90,
		                        halign:'center',
		                        formatter : function(value, row, index) {
		         		        	switch (value) {
		         					case "0":
		         						return '正常';
		         					case "1":
		         						return '停用';
		         					}
		         				}
		                    },{
		                        field:'parentId',
		                        title:'上级字典',
		                        hidden:'true'
		                    },{
		         				field:'action',
		        		        title:'操作',
		        		        width:110,
		        		        halign:'center',
		        		        formatter : function(value, row, index) {
		        					var str = '&nbsp;';
		        					<sec:authorize ifAnyGranted='B_ZDGL_EDIT'>
		        						str += $.formatString('<a href="javascript:void(0)" onclick="editSubDict(\'{0}\',\'{1}\');" >编辑</a>', mainIndex, index);
		        						str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
		        					</sec:authorize>
		        					<sec:authorize ifAnyGranted='B_ZDGL_DEL'>
		        						str += $.formatString('<a href="javascript:void(0)" onclick="delSubDict(\'{0}\',\'{1}\');" >删除</a>', mainIndex, row.id);
		        					</sec:authorize>
		        					return str;
		        				}
		        			 }]],
		                     onResize:function(){
		                         $('#dict_list').datagrid('fixDetailRowHeight',index);
		                     },
		                     onLoadSuccess:function(){
		                         setTimeout(function(){
		                             $('#dict_list').datagrid('fixDetailRowHeight',index);
		                         },0);
		                     }
		                });
		                $('#dict_list').datagrid('fixDetailRowHeight',index);
		         	}
		       	});
			})
		</script>
	</head>
	
	<body>
		<div class="easyui-layout" data-options="fit:true,border:false">
			<div data-options="region:'center',border:false" >
				<table id="dict_list"></table>
			</div>
		</div>
		<div id="toolbar" style="display:none;">
			<sec:authorize ifAnyGranted='B_ZDGL_ADD'>
				<a onclick="addDict('root');" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">添加</a>
			</sec:authorize>
		</div>
		
		<!-- 新增字典dialog -->
		<div id="dlg" class="easyui-dialog" style="width:600px;height:auto;padding:10px 20px" buttons="#dlg-buttons" data-options="closed:true,modal:true">
			<form id="dictForm" method="post" novalidate="false">
				<input type="hidden" name="id"/>
				<input type="hidden" id="parent_id"  name="parentId"/>	
		 		<table class="grid">
		 			<tr>
		 				<td>字典名称：</td>
		 				<td><input type="text" class="easyui-textbox" name="dictName" data-options="required:true"/></td>
		 				<td>字典编码：</td>
		 				<td><input type="text" class="easyui-textbox" name="dictCode" data-options="required:true"/></td>
		 			</tr>
		 			<tr>
		 				<td>字典值：</td>
		 				<td><input type="text" value="0" class="easyui-numberspinner" name="dictValue" style="width: 100px;" data-options="min:0,max:99"/></td>
		 				<td>字典状态：</td>
		 				<td>
							<select name="dictStatus" class="easyui-combobox" style="width: 100px;" data-options="editable:false,panelHeight:'auto'">
								<option value="0">正常</option>
								<option value="1">停用</option>
							</select>
						</td>
		 			</tr>
		 			<tr>
		 				<td>备注：</td>
		 				<td colspan="3"><input type="text" name="remark" class="easyui-textbox" data-options="multiline:true,height:50" style="width:300px;" /></td>
		 			</tr>
		 		</table>
		 	</form>
		</div>
		<div id="dlg-buttons">
			<a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="javascript:saveDict();" style="width:90px;">保存</a>
		    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close');" style="width:90px;">取消</a>
		</div>
	</body>
</html>

<script type="text/javascript">
	//===============================字典增删改=================================
	function addDict(parentid){
		$('#dictForm').form('clear');
		$('#dictForm').form('load',{
			dictValue:0,
			dictStatus:0
		});
		$('#parent_id').val(parentid);
		$('#dlg').dialog('open').dialog('setTitle','新增字典');
	}
	
	function editDict(rowIndex){
		$('#dict_list').datagrid('selectRow',rowIndex);
		var row = $('#dict_list').datagrid('getSelected');
		if (row){
			$('#dlg').dialog('open').dialog('setTitle','修改字典');
			$('#dictForm').form('load',row);
		}else{
			$.messager.alert('提示','请选择一条记录','info');
		}
	}
	
	function saveDict(){
		var data = form2Json("dictForm");
	    if($("#dictForm").form('validate')){
	        $.post('dict_save',{data:data},function(result) {
	            if(result.success){
	            	$('#dlg').dialog('close');
	                $.messager.show({
	                    title: '提示',
	                    msg: result.msg
	                });
	                if($('#parent_id').val() != 'root') {
						$('#ddv-'+mainIndex).datagrid('reload');
					} else {
						$('#dict_list').datagrid('reload');
					}
	            }else{
	                $.messager.show({
	                    title: '提示',
	                    msg: result.msg
	                });
	            }
	        },'json');
	    }
	}
	
	function destroyDict(id){
		$.messager.confirm('提示','确定删除?',function(r){
			if (r){
				$.ajax({
					url:'delete',
					type:'POST',
					dataType:'json',
					data:{id:id},
					success: function(result){
						if(result.success) {
							$.messager.show({
								title: '提示',
								msg: result.msg
							});
							$('#dict_list').datagrid('reload');
						} else {
							$.messager.show({
								title: '提示',
								msg: result.msg
							});
						}
					}
				});
			}
		});
	}
	
	
	//===============================子字典删改=================================
	function editSubDict(id,rowId){
		$('#ddv-'+id).datagrid('selectRow',rowId);
		var row = $('#ddv-'+id).datagrid('getSelected');
		$('#dlg').dialog('open').dialog('setTitle','修改字典');
		$('#dictForm').form('load',row);
	}
	
	function delSubDict(id,idd){
		$.messager.confirm('提示','确定删除?',function(r){
			if (r){
				$.ajax({
					url:'delete',
					type:'POST',
					dataType:'json',
					data:{id:idd},
					success: function(result){
						if(result.success) {
							$.messager.show({
								title: '提示',
								msg: result.msg
							});
							$('#ddv-'+id).datagrid('reload');
						} else {
							$.messager.show({
								title: '提示',
								msg: result.msg
							});
						}
					}
				});
			}
		});
	}
</script>